import { IRect, Rect } from '../base/Rect.js';
import { FISprite } from './FISprite.js';

export class FIImage extends FISprite {
  protected _source?: CanvasImageSource | undefined;
  protected _sourceRect: Rect = new Rect(0, 0, 0, 0);
  get sourceRect(): IRect {
    return this._sourceRect;
  }
  set sourceRect(v: IRect) {
    this._sourceRect.set(v);
  }
  setSourceRect(v: IRect): FIImage {
    this.sourceRect = v;
    return this;
  }
  get source(): CanvasImageSource | undefined {
    return this._source;
  }
  set source(v: CanvasImageSource | undefined) {
    this._source = v;
  }
  setSource(v: CanvasImageSource | undefined): FIImage {
    this.source = v;
    return this;
  }
  draw(ctx: CanvasRenderingContext2D): void {
    if (!this._source) {
      return;
    }
    ctx.drawImage(
      this._source,
      this._sourceRect._x,
      this._sourceRect._y,
      this._sourceRect._w,
      this._sourceRect._h,
      - this._ax,
      - this._ay,
      this._w,
      this._h
    );
  }
}
